package com.ea.eadp.nexus.test.pageDefinition.originX;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;

public class SignupPage extends BasePage {

    private static final String defaultUri = baseUri + TestUtil.SignupOriginXConnectURI();

    private static final String EMAIL_TEXTBOX_ID = "email";
    private static final String PASSWORD_ID = "password";
    private static final String FISRTNAME_ID = "firstName";
    private static final String LASTNAME_ID = "lastName";
    private static final String ID_ID = "originId";

    private static final String TOS_PATH = "//div[@id='read-accept-container']/span[1]/label";

    public SignupPage(final WebDriver driver) {
        super(driver, SignupPage.class);
    }

    @Override
    public void navigate() {
        navigate(defaultUri);
    }

    @Override
    public void navigate(String uri) {
        driver.switchTo().window(operationWindowHandle);
        log.info(uri);
        driver.navigate().to(uri);
    }

    public void selectCountry(String country) {
        driver.switchTo().window(operationWindowHandle);
        driver.findElement(By.id("clientreg_country-selctrl")).click();
        String cssSelectorValue = "//option[@value='" + country + "']";
        driver.findElement(By.xpath(cssSelectorValue)).click();
    }

    public void selectDOBMonth(final int month) {
        driver.switchTo().window(operationWindowHandle);
        driver.findElement(By.id("clientreg_dobmonth-selctrl")).click();
        String cssSelectorValue = "//select[@id='clientreg_dobmonth-selctrl']/option[@value='" + month + "']";
        driver.findElement(By.xpath(cssSelectorValue)).click();
    }

    public void selectDOBDay(final int day) {
        driver.switchTo().window(operationWindowHandle);
        driver.findElement(By.id("clientreg_dobday-selctrl")).click();

        String cssSelectorValue = "//select[@id='clientreg_dobday-selctrl']/option[@value='" + day + "']";
        driver.findElement(By.xpath(cssSelectorValue)).click();
    }

    public void selectYear(final int year) {
        driver.switchTo().window(operationWindowHandle);
        driver.findElement(By.id("clientreg_dobday-selctrl")).click();
        String cssSelectorValue = "//select[@id='clientreg_dobyear-selctrl']/option[@value='" + year + "']";
        driver.findElement(By.xpath(cssSelectorValue)).click();
    }

    public void checkTOS(final Boolean checked) {
        String classes = driver.findElement(By.id("readAccept")).getAttribute("class");
        if (checked ^ classes.contains("checked")) {
            driver.findElement(By.xpath(TOS_PATH)).click();
        }
    }

    public void validateFirstNextButtonIsDisabled() {
        Assert.assertTrue(driver.findElement(By.id("countryDobNextBtn")).getAttribute("class").contains("disabled"));
    }

    public void clickFirstNextButton() {
        driver.findElement(By.id("countryDobNextBtn")).click();
    }

    public void waitForLoadingSecondPage() {

    }

    public void inputEmail(final String email) {
        PageOperation.SendKeyToElementAfterWait(driver, EMAIL_TEXTBOX_ID, email);
    }

    public void inputPassword(final String password) {
        PageOperation.SendKeyToElementAfterWait(driver, PASSWORD_ID, password);
    }

    public void inputPublicID(final String publicID) {
        PageOperation.SendKeyToElementAfterWait(driver, ID_ID, publicID);
    }

    public void showPassword(final Boolean show) {

    }

    public void inpuFirstName(final String firstName) {
        PageOperation.SendKeyToElementAfterWait(driver, FISRTNAME_ID, firstName);
    }

    public void inputLastName(final String lastName) {
        PageOperation.SendKeyToElementAfterWait(driver, LASTNAME_ID, lastName);
    }

    public void inputRecapcha() {
        PageOperation.SendKeyToElementAfterWait(driver, "recaptcha_response_field", "a");
    }

    public void validateSecondNextButtonIsDisabled() {
        Assert.assertTrue(driver.findElement(By.id("basicInfoNextBtn")).getAttribute("class").contains("disabled"));
    }

    public void clickSecondNextButton() {
        driver.findElement(By.id("basicInfoNextBtn")).click();
    }

    public void clickCreateAccount() {
        driver.findElement(By.id("submitBtn")).click();
    }

    public void waitForCreateAccount() {
        PageOperation.WebDriverWait(driver, "createEndForm");
    }

    public void validateSignupInfo(String publicID) {
        Assert.assertEquals(driver.findElement(By.xpath("//form[@id='createEndForm']/h1")).getText(), String.format("Welcome, %s!", publicID));
    }

    public void clickLoginButton() {
        driver.findElement(By.id("signIn")).click();
    }

    public void validateInvalidPassword() {
        validateErrorMassage("form-error-password-complexity", "Password does not meet requirements.");
    }

    public void validateTooShortPublicId() {
        validateErrorMassage("form-error-origin-id-too-short", "ID must be between 4 and 16 characters.");
    }

    public void validateTooLongPublicId() {
        validateErrorMassage("form-error-origin-id-too-long", "ID must be between 4 and 16 characters.");
    }

    public void validateInvalidPublicId() {
        validateErrorMassage("form-error-origin-id-invalid", "ID contains a prohibited word or character");
    }

    public void validateNoFirstname() {
        try {
            if (driver.findElement(By.id(FISRTNAME_ID)).isDisplayed()) {
                Assert.fail("Firstname textbox should not exist.");
            }
        } catch (NoSuchElementException e) {
        }
    }

    public void validateNoLastname() {
        try{
            if (driver.findElement(By.id(LASTNAME_ID)).isDisplayed()) {
                Assert.fail("Lastname textbox should not exist.");
            }
        } catch (NoSuchElementException e) {
        }
    }

}